package com.example.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class PostHttpRequest {

    public static void main(String[] args) throws IOException {
        String url = "https://medium.com/_/graphql";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // 设置请求方法为POST
        con.setRequestMethod("POST");

        // 添加必要的HTTP头部信息，例如Content-Type（根据实际API要求设置）
        con.setRequestProperty("Content-Type", "application/json; charset=utf-8");

        // 声明允许输出
        con.setDoOutput(true);

        // 构建请求参数
        String postParams = "operationName=CuratedHomeFeedModuleQuery&param2=value2"; // 根据API需要调整

        try(OutputStream os = con.getOutputStream()) {
            byte[] input = postParams.getBytes("utf-8");
            os.write(input, 0, input.length);           
        }

        int responseCode = con.getResponseCode();
        System.out.println("Response Code : " + responseCode);

        try(BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印结果
            System.out.println(response.toString());
        }
    }
}